<?php

namespace app\api\controller\logistics;

use app\common\controller\qyg\Api;
use think\Exception;
use think\Request;

class Order extends Api
{
    public $noNeedRight = "*";

    public $noNeedLogin = ["kd_list","cfg"];

    public $service;

    public function __construct()
    {
        parent::__construct();
        $this->service = new \app\common\service\logistics\Order();
    }

    public function kd_list()
    {
        try {
            $this->success("ok",$this->service->kd_list());
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    public function cfg()
    {
        try {
            $this->success("ok",$this->service->cfg());
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    public function sending(Request $request)
    {
        try {
            $this->success("ok",$this->service->sending($this->auth,$request));
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    public function rob(Request $request)
    {
        $oid     = $request->param("oid");
        $lock_key= "order".$oid;
        try {
            $lock    = $this->redis->handler()->setnx($lock_key,1);
            if(!$lock){
                throw new Exception("操作频繁");
            }
            $r = $this->service->rob($this->auth,$oid);
            $this->redis->handler()->del($lock_key);
            $this->success("ok",$r);
        }catch (Exception $e){
            $this->redis->handler()->del($lock_key);
            $this->error($e->getMessage());
        }
    }

    public function picked(Request $request)
    {
        try {
            $this->success("ok",$this->service->picked($this->auth,$request->param("oid")));
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    public function finish(Request $request)
    {
        try {
            $this->success("ok",$this->service->finish($this->auth,$request));
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    public function express_list(Request $request)
    {
        try {
            $this->success("ok",$this->service->express_list($this->auth,$request));
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    public function express_detail(Request $request)
    {
        try {
            $this->success("ok",$this->service->express_detail($this->auth->id,$request->param("oid")));
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    public function user_list(Request $request)
    {
        try {
            $this->success("ok",$this->service->user_list($this->auth,$request));
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    public function user_detail(Request $request)
    {
        try {
            $this->success("ok",$this->service->user_detail($this->auth->id,$request->param("oid")));
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }
}